<?php
defined('_JEXEC') or die();

class WrdControllerExchange extends JControllerLegacy 
{
        protected  $option;
        protected  $controller;
        
		function __construct( $default = array()){
		
    		$this->option = 'com_wrd';
    		$this->controller = 'exchange';
			parent::__construct( $default );
			
    	}
      	function display()
 		{
			
			switch($this->getTask()){
				
				case 'add':{
					
					$user = JFactory::getUser();
					JRequest::setVar( 'hidemainmenu', 1 );
					JRequest::setVar( 'layout', 'form'  );
					JRequest::setVar( 'view'  , 'exchange');
					
				} 
				break;
	            default:
					JRequest::setVar( 'view'  , 'exchange');
	            	break;
			}
			parent::display();
        }
        
      //save profile info
    function save(){
    	
       $post = JRequest::get('post');
       $post['point'] = abs(intval($post['point']));
       $post['type'] = 2 ;
       $post['outlet'] = 'wmc' ;
	   
       $user_info = WrdHelper::getUserProfile($post['member_id'],'member_id') ;
       
	   unset($post['member_id']);
       $post['user_id'] = $user_info->user_id ;
       
       if(!empty($post['created_id'])){
	   
       		unset($post['created_id']) ;
       		unset($post['created_date']) ;
       }else{
       		$post['created_date'] = date('Y-m-d H:i:s') ;	
       		$post['created_id'] = $post['modify_id'] ;
       }
       
       if(strtotime($post['expire']) < time()){
       		$post['expired'] = 1;
       }
       
       $post['modify_date'] = date('Y-m-d') ;
       $post['transaction'] = '2'.substr(time(), 1);
       
       $params = JComponentHelper::getParams('com_wrd');
       $model=$this->getModel('exchange');
       
	   //get date current
       $id_category=$model->store($post,'exchange');
       if($id_category!=0){

			$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['profile_id'].'&layout=form&tab=point','Saved');
			return true;           
       }else{
            $this->setRedirect('index.php?option=com_wrd&controller=exchange','Error Saved');
            return false;
       }
   }    

   function cancel(){
		$post=JRequest::get('post');
		
		$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['profile_id'].'&layout=form&tab=point');
	}
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('exchange');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=exchange' );
	}
}